package com.fruit.app.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

public class DateUtil {
	
	public static String getSerial(Date date,int index){
		long msel=date.getTime();		
		SimpleDateFormat fm=new SimpleDateFormat("MMddyyyyHHmmssSS");		
		msel+=index;					
		date.setTime(msel);				
		String serials=fm.format(date);	
		return serials;
	}	
	
	public static Date getStartDate(int span) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - span);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 获取当前日期的前后n天
	 * @author ZuoChangjun 2012-2-29
	 */
	public static Date getDate(Date curDate, int n) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		calendar.setTime(curDate);
        calendar.add(Calendar.DATE, n);    
        return calendar.getTime();
	}
	
	public static List<Date> getDateSpan(Date start, Date end) {		
		List<Date> dates = new ArrayList<Date>();
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(end);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);	

		Date curDate = start;
		
		while(true) {
			if(curDate.after(end)) {
				break;
			}
			cal = Calendar.getInstance();
			cal.setTime(curDate);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			dates.add(cal.getTime());			
			
			cal = Calendar.getInstance();
			cal.setTime(curDate);
			cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
			curDate = cal.getTime();
		}		
		return dates;	
	}
	
	@SuppressWarnings("static-access")
	public static String getTimeZoneTime(String timeZone,String format){
		
		TimeZone t = TimeZone.getTimeZone(timeZone);
		t.setDefault(t);
		Calendar cal = Calendar.getInstance(t);
		System.out.println(cal.getTimeZone().getID());
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 判断当前时间是否介于startTime和endTime之间
	 * @param startTime 格式：HH:MM:SS
	 * @param endTime  格式：HH:MM:SS
	 * @param timeZoneID
	 * @param languageCode
	 * @return
	 */
	public static boolean isCurrentTimeInRange(String startTime,String endTime,String timeZoneID,String languageCode) {
		if(StringUtils.isBlank(timeZoneID)){
			timeZoneID="GMT+8";
		}
		if(StringUtils.isBlank(languageCode)){
			languageCode="zh";
		}
		if(timeZoneID.indexOf("GMT")<0 &&  timeZoneID.indexOf("+")<0 && timeZoneID.indexOf("-")<0 ){
			timeZoneID="GMT+"+timeZoneID;
		}
		if(timeZoneID.indexOf("GMT")<0 &&  (timeZoneID.indexOf("+")>=0 || timeZoneID.indexOf("-")>=0 )){
			timeZoneID="GMT"+timeZoneID;
		}
		//timeZoneID="Etc/"+timeZoneID;
		//System.out.println("调用isCurrentTimeInRange方法前，默认时区="+TimeZone.getDefault().toString());
		Locale locale=new Locale(languageCode,"","");
		TimeZone tz = TimeZone.getTimeZone(timeZoneID);
		TimeZone defaultTZ=TimeZone.getDefault();//备份默认时区
		TimeZone.setDefault(tz);//切换时区		
		Calendar curCal = Calendar.getInstance(tz,locale);//当前日历
		Calendar startCal = Calendar.getInstance(tz,locale);
		int startTimeInt=Integer.parseInt(StringUtils.substring(startTime,0, startTime.indexOf(":")));

	    startCal.set(Calendar.HOUR, startTimeInt%12);
		startCal.set(Calendar.HOUR_OF_DAY, startTimeInt);
		startCal.set(Calendar.MINUTE, Integer.parseInt(StringUtils.substring(startTime, startTime.indexOf(":")+1,startTime.length())));
		
		Calendar endCal = Calendar.getInstance(tz,locale);
		int endStartTime=Integer.parseInt(StringUtils.substring(endTime,0, endTime.indexOf(":")));
		endCal.set(Calendar.HOUR, endStartTime%12);
		endCal.set(Calendar.HOUR_OF_DAY, endStartTime);
		endCal.set(Calendar.MINUTE, Integer.parseInt(StringUtils.substring(endTime, endTime.indexOf(":")+1,endTime.length())));
		TimeZone.setDefault(defaultTZ);//恢复默认时区
		//System.out.println("调用isCurrentTimeInRange方法后，默认时区="+TimeZone.getDefault().toString());
		//如果当前时间介于startTime和endTime之间，则返回true
		if (curCal.before(endCal) && curCal.after(startCal)) {
			return true;
		}else {
			return false;
		}
	}
	/**  
	* 日期转换成字符串  
	* @param date   
	* @return str  
	* @author ZuoChangjun 2012-2-29
	*/  
	public static String dateToStr(Date date,String formatStr) {   
	   if(StringUtils.isBlank(formatStr)){
		   formatStr="yyyy-MM-dd HH:mm:ss";
	   }
	   SimpleDateFormat format = new SimpleDateFormat(formatStr);   
	   String str = format.format(date);   
	   return str;   
	}    
	  
	/**  
	* 字符串转换成日期  
	* @param str  
	* @return date  
	* @author ZuoChangjun 2012-2-29
	*/  
	public static Date strToDate(String str,String formatStr) {   
		if (StringUtils.isBlank(formatStr)) {
			formatStr = "yyyy-MM-dd HH:mm:ss";
		}
	   SimpleDateFormat format = new SimpleDateFormat(formatStr);   
	   Date date = null;   
	   try {   
	    date = format.parse(str);   
	   } catch (ParseException e) {   
	    e.printStackTrace();   
	   }   
	   return date;   
	}  
	/**
	 * 求两个日期相差的天数
	 * @param now
	 * @param returnDate
	 * @return
	 * @author ZuoChangjun 2012-2-29
	 */
	public static  int getDaysBetween(Date endDate) {
		
		Date startDate=strToDate("2012-02-01 00:00:00","yyyy-MM-dd HH:mm:ss");//相对日期
		return getDaysBetween(startDate,endDate);
	}
	/**
	 * 求两个日期相差的天数
	 * @param now
	 * @param returnDate
	 * @return
	 * @author ZuoChangjun 2012-2-29
	 */
	public static int getDaysBetween(Date startDate, Date endDate) {
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		endCalendar.setTime(endDate);
		setTimeToMidnight(startCalendar);
		setTimeToMidnight(endCalendar);

		// 过滤掉时分秒
		long startMs = startCalendar.getTimeInMillis();
		// 过滤掉时分秒
		long endMs = endCalendar.getTimeInMillis();
		long intervalMs = endMs - startMs;
		return millisecondsToDays(intervalMs);
	}

	/**
	 * 毫秒转化为天数
	 * @param intervalMs
	 * @return
	 * @author ZuoChangjun 2012-2-29
	 */
	public static  int millisecondsToDays(long intervalMs) {
		return (int) (intervalMs / (1000 * 86400));
	}

	/**
	 * 过滤时分秒
	 * @param calendar
	 * @author ZuoChangjun 2012-2-29
	 */
	public static  void setTimeToMidnight(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
	}

	/**
	 * 获取当前日期的前一天
	 * @param calendar
	 * @author ZuoChangjun 2012-2-29
	 */
	public static String getPrevDateStrOfCurDate() {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.DATE, -1);    //得到前一天
        return  new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
	}

	
	public static void main(String[] args) {
//		System.out.println(getDaysBetween(new Date()));
//		System.out.println(DateUtil.getDaysBetween(new Date())%2==0?3:4);
//		DateUtil ut = new DateUtil();
//		Calendar date = Calendar.getInstance();		
//		//date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
//		date.set(Calendar.MONTH, 5);
//		date.set(Calendar.DATE, 2);
//		date.set(Calendar.HOUR_OF_DAY, 15);
//		date.set(Calendar.MINUTE, 0);
//		date.set(Calendar.SECOND, 0);		
//		Date startDate = date.getTime();
//		date = Calendar.getInstance();
//		//date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
//		date.set(Calendar.MONTH, 5);
//		date.set(Calendar.DATE, 2);
//		date.set(Calendar.HOUR_OF_DAY, 15);
//		date.set(Calendar.MINUTE, 0);
//		date.set(Calendar.SECOND, 0);
//		Date endDate = date.getTime();
//		System.out.println("Days between " + startDate + " and " + endDate);
//		System.out.println("--------------------");
//		List<Date[]> dateSpan = ut.getDateSpan(startDate, endDate);
//		for(Date[] d : dateSpan) {
//			System.out.println("start: " + d[0]);
//			System.out.println("end: " + d[1]);
//			System.out.println("--------------------");		
//		}
		
		TimeZone t = TimeZone.getTimeZone("GMT+8");
		
		//t.setDefault(t);
		
		Calendar cal = Calendar.getInstance(t);
		
		cal.set(Calendar.HOUR_OF_DAY, 22);
		
		cal.set(Calendar.MINUTE, 22);
		
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime()));
		
		Calendar cal1 = Calendar.getInstance(t);
		
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal1.getTime()));
		
		//之前
		System.out.println(cal.before(cal1));
		//之后
		System.out.println(cal.after(cal1));
		
	}
}
